using System;
using System.Collections.Generic;
using LitJson;

namespace Battle.TimelineLog
{

    /// <summary>
    /// 静态数据管理
    /// </summary>
    public class SysDataMgr
    {
        private static SysDataMgr instance = null;
        public static SysDataMgr Inst { get { if (instance == null) instance = new SysDataMgr(); return instance; } }

        private Dictionary<string, object> sysData { get; }
        private Dictionary<string, string> m_TableData;
        public SysDataMgr()
        {
            sysData = new Dictionary<string, object>();
            m_TableData = new Dictionary<string, string>();
        }

        public void StartLoad()
        {
            var buffer = FileLoader.ReadBinFile("bin/table/data.bytes");
            var ms = new MMO_MemoryStream(buffer);
            uint headLen = ms.ReadUInt(); //表数量
            string head = ms.ReadUTFBytes((int)headLen);
            int headIdx = (int)headLen + 4;
            Dictionary<string, int[]> data = JsonMapper.ToObject<Dictionary<string, int[]>>(head);
            foreach (string key in data.Keys)
            {
                int[] value = data[key];
                int index = value[0];
                int size = value[1];
                ms.Position = index + headIdx;
                string table = ms.ReadUTFBytes(size);
                m_TableData.Add(key, table);
            }

            // Register
            RegisterAll();
        }

        public void RegistData<T>(string key) where T : ISysMainKey
        {
            var dataHash = new SysHashList<T>();
            sysData.Add(key, dataHash);
        }

        private SysHashList<T> getData<T>(string key) where T : ISysMainKey
        {
            object dataHash;
            if (sysData.TryGetValue(key, out dataHash))
            {
                var data = dataHash as SysHashList<T>;
                if (!data.HasInit)
                {
                    string str;
                    m_TableData.TryGetValue(key, out str);
                    if (str != null) data.ParserData(str);
                }
                return data;
            }
            return null;
        }

        void RegisterAll()
        {
            RegistData<SysSkill>(BSysName.Sys_Skill);
            RegistData<SysBuff>(BSysName.Sys_Buff);
            RegistData<SysHero>(BSysName.Sys_Hero);
            RegistData<SysProp>(BSysName.Sys_Prop);
            RegistData<SysSkillCondition>(BSysName.Sys_SkillCondition);
            RegistData<SysSkillEffect>(BSysName.Sys_SkillEffect);
        }
        public static SysHashList<SysSkill> Sys_Skill { get { return Inst.getData<SysSkill>(BSysName.Sys_Skill); } }
        public static SysHashList<SysBuff> Sys_Buff { get { return Inst.getData<SysBuff>(BSysName.Sys_Buff); } }
        public static SysHashList<SysHero> Sys_Hero { get { return Inst.getData<SysHero>(BSysName.Sys_Hero); } }
        public static SysHashList<SysProp> Sys_Prop { get { return Inst.getData<SysProp>(BSysName.Sys_Prop); } }
        public static SysHashList<SysSkillCondition> Sys_SkillCondition { get { return Inst.getData<SysSkillCondition>(BSysName.Sys_SkillCondition); } }
        public static SysHashList<SysSkillEffect> Sys_SkillEffect { get { return Inst.getData<SysSkillEffect>(BSysName.Sys_SkillEffect); } }

    }
    public class BSysName
    {
        public static string Sys_Skill = "Skill.json";
        public static string Sys_Buff = "Buff.json";
        public static string Sys_Hero = "Hero.json";
        public static string Sys_Prop = "Prop.json";
        public static string Sys_SkillCondition = "SkillCondition.json";
        public static string Sys_SkillEffect = "SkillEffect.json";
    }
}